package project;
import java.sql.*;

public class ClientManager
{
	private String input = null, type = null;
	Client client;
	Connection conn;
	
	public ClientManager()
	{
		try
		{
			String userid, password;
			userid = "root";
			password = "";
			MySQL_Connection msc = new MySQL_Connection(
					"turtleData","localhost",userid, password);
			conn = msc.getMySQLConnection();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public void commitClient(Client client)
	{
		try
		{
			this.client = client;
			String [] str = client.getClientString();
			Statement stmt = conn.createStatement();
			String update;
			for(int i = 0; i < str.length; i++)
			{
				System.out.println(str[i]);
			}
			
			//Must fix this code with proper get methods
			update = "INSERT INTO customerTable VALUES('"+
				str[0]+"','"+str[1]+"','"+str[2]+"','"+str[3]+"','"+
				str[4]+"','"+str[5]+"','"+str[6]+"','"+str[7]+"','"+
				str[8]+"','"+str[9]+"','"+str[10]+"','"+str[11]+"','"+
				str[12]+"','"+str[13]+"','"+str[14]+"','"+
				str[15]+"','"+str[16]+"','"+str[17]+"','"+
				str[18]+"')";
			int rows = stmt.executeUpdate(update);
			System.out.println("Rows changed: "+rows);
		}
		catch(SQLException sqlx)
		{
			sqlx.printStackTrace();
		}
	}

	
	public String generateClientId()
	{
		try
		{
			String type = surname;
			Statement stmt = conn.createStatement(
						ResultSet.TYPE_SCROLL_SENSITIVE,
						ResultSet.CONCUR_READ_ONLY);
			ResultSet rs;
			String query = "SELECT custId from customerTable";
			rs = stmt.executeQuery(query);
			rs.last();
			int rows = rs.getRow();
			rs.first();
			
			for(int i = 0; i < rows; i++)
			{
				String surSub = lastName.substring(0,3)+""+100+""+(i+1);
				surSub = surSub.toLowerCase();
				System.out.println(surSub);
				
				String subSurSub = 
					""+surSub.substring(0,3)+surSub.substring(4);
				if(subSurSub.equals(rs.getString("custId")))
				{
					rs.next();
					continue;
				}
				else
				{
					//rs.close();
					return subSurSub;
				}
			}
			//rs.close();
		}
		catch(SQLException sqlx)
		{
			sqlx.printStackTrace();
		}
		return "Error";
	}
}